﻿#include "FITKNastranBDFWriter.h"
#include "FITKNastranBDFIOTool.h"
#include "FITKNastranAdaptorCase.h"
#include "FITKNastranBDFIOInterface.h"

#include "FITK_Component/FITKAbaqusData/FITKDataCase.h"
#include "FITK_Component/FITKAbaqusData/FITKAbaqusPart.h"
#include "FITK_Kernel/FITKAdaptor/FITKIOAdaptorFactory.h"
#include "FITK_Kernel/FITKAppFramework/FITKMessage.h"

namespace Nastran
{
    FITKNastranBDFWriter::FITKNastranBDFWriter(bool & success)
        :_isSuccess(success)
    {
        _writerTool = new FITKNastranBDFIOWriterTool(this);
    }

    FITKNastranBDFWriter::~FITKNastranBDFWriter()
    {
        if (_writerTool) delete _writerTool;
    }

    void FITKNastranBDFWriter::run()
    {
        _isSuccess = this->writer();
        
        if (!_isSuccess)
            consoleMessage(3, "error adapting for case");
        else
            consoleMessage(1, QString("write BDF File succeed ! %1").arg(_fileName));

        // 关闭文件流
        this->closeFileStream();
    }

    void FITKNastranBDFWriter::consoleMessage(int level, const QString &str)
    {
        switch (level)
        {
        case 1:
            AppFrame::FITKMessageNormal(str);
            break;
        case 2:
            AppFrame::FITKMessageWarning(str);
            break;
        case 3:
            AppFrame::FITKMessageError(str);
            break;
        default:
            AppFrame::FITKMessageError(str);
            break;
        }
    }

    FITKNastranBDFIOWriterTool * FITKNastranBDFWriter::getWriterTool()
    {
        return _writerTool;
    }

    bool FITKNastranBDFWriter::writer()
    {
        //获取算例数据
        AbaqusData::FITKDataCase* caseData = dynamic_cast<AbaqusData::FITKDataCase*>(_data);
        // 打开文件流
        if (nullptr == caseData || !(this->openFileStream()))
            return false;
        //获取part管理器
        AbaqusData::FITKAbaqusPartManager *partManager = caseData->getPartManager();
        if (nullptr == partManager)
        {
            this->consoleMessage(2, "nullptr error for part manager");
            return false;
        }
        //获取part数据
        AbaqusData::FITKAbaqusPart* mesh = partManager->getDataByIndex(0);
        if (!mesh) return false;
        //设置工具数据
        _writerTool->setCaseData(caseData);
        _writerTool->setPartData(mesh);
        _writerTool->updateData();
        //创建读写适配器
        auto adaptor = FITKIOADAPTORFACTORY->createT<Nastran::FITKNastranAdaptorCase>("BDF", "NastranCase");
        // Check if the adaptor is null, if so, log an error message
        if (nullptr == adaptor)
        {
            consoleMessage(3, "null ptr error for FITKNastranAdaptorCase");
            return false;
        }
        adaptor->setWriter(this);
        adaptor->setCaseData(caseData);
        adaptor->setPartData(mesh);
        // Attempt to adapt the data, if it fails, log an error message
        _isSuccess = adaptor->adaptW();
        // Delete the adaptor to free memory
        delete adaptor;
        return _isSuccess;
    }
}

